package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.domain.UserAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by DK on 17/3/17.
 */
@Repository
public class UserAdminRepositoryImpl implements UserAdminRepositoryCustom{

    private static final Logger log = LoggerFactory.getLogger(UserAdminRepositoryImpl.class);

    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<UserAdmin> findUserAdminPage(Long titleId, PageResult page) {
        SQLPageDoing<UserAdmin> doing = (sql, fields, params, map) -> {
            sql.append("select %s from user_admin ua where 1=1 and is_delete='NO'");
            if (titleId != null) {
                sql.append("and ua.title_id = :titleId");
                params.put("titleId", titleId);
            }
            sql.append(" order by ua.last_updated desc");
            fields.append("ua.*");
        };
        return doing.go(page, UserAdmin.class, objectMapper, jdbcTemplate,log);
    }
}
